package com.lams.service.archive.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.lams.jdbc.QueryUtils;
import com.lams.service.BaseFacade;
import com.lams.service.archive.ISchoolFacade;
import com.lams.util.IdFactory;
import com.lams.util.ResultEntity;
import static com.lams.util.ViewParamsUtil.*;

public class SchoolFacade extends BaseFacade implements ISchoolFacade {
	private String querySchool;
	private String insertSchool;
	private String updateSchool;
	private String deleteSchool;
	private static final String COLUMN_KEY = "SCHOOL_ID";
	private static final String TABLE_NAME = "C_SCHOOL";

	/* (non-Javadoc)
	 * @see com.lams.service.archive.impl.ISchoolFacade#querySchool(int, int, java.util.Map)
	 */
	@Override
	public ResultEntity querySchool(int start, int limit, Map params) {
		String sql = QueryUtils.getSqlBySqlCode(querySchool, params);
		ResultEntity result = new ResultEntity(QueryUtils.executeQuery(start,
				limit, sql), QueryUtils.getTotalCount(sql));
		return result;
	}

	/* (non-Javadoc)
	 * @see com.lams.service.archive.impl.ISchoolFacade#saveOrUpdate(java.util.Map)
	 */
	@Override
	public ResultEntity saveOrUpdate(Map params){
		String school_id = getParam(COLUMN_KEY);
		String sql = "";
		if (school_id == null || school_id.trim().equals("")) {
			params.put(COLUMN_KEY, IdFactory.getNextNav(TABLE_NAME, COLUMN_KEY));
			sql = QueryUtils.getSqlBySqlCode(insertSchool, params);
		}else {
			sql = QueryUtils.getSqlBySqlCode(updateSchool, params);
		}
		ResultEntity result = new ResultEntity();
		try {
			super.getDao().executeUpdate(sql);
			result.setSuccess(true);
			result.setMessage("保存成功");
		} catch (SQLException e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage("保存失败");
		}
		return result;
	}
	/* (non-Javadoc)
	 * @see com.lams.service.archive.impl.ISchoolFacade#delete(java.lang.String)
	 */
	@Override
	public boolean delete(String school_id){
		Map params = new HashMap();
		params.put(COLUMN_KEY, school_id);
		try {
			super.getDao().executeUpdate(QueryUtils.getSqlBySqlCode(deleteSchool, params));
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	public void setQuerySchool(String querySchool) {
		this.querySchool = querySchool;
	}
	public void setInsertSchool(String insertSchool) {
		this.insertSchool = insertSchool;
	}
	public void setUpdateSchool(String updateSchool) {
		this.updateSchool = updateSchool;
	}

	public void setDeleteSchool(String deleteSchool) {
		this.deleteSchool = deleteSchool;
	}
	
}
